package com.invengo.module_check.ui.fragment.select_layer;

import static com.invengo.module_check.data.entity.ActionArgs.ACTION_CHANGE;
import static com.invengo.module_check.data.entity.ActionArgs.ACTION_FORCE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_BOOKS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_ACTION;
import static com.invengo.module_check.data.entity.BundleKey.KEY_OP_STATE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELF_CODE;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHOW_SUPPORT;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.invengo.device.DeviceHelper;
import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentSelectLayerBinding;
import com.invengo.module_check.ui.fragment.widget.SelectShelfDialog;

import java.io.Serializable;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * 废弃，使用SelectShelfUtils 来选层架
 */
public class SelectLayerFragment extends BaseFragment<FragmentSelectLayerBinding, SelectLayerViewModel> {
    ActionArgs actionArgs;
    List<BookBean> bookBeans;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_select_layer;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initData() {
        viewModel.newLive();
        actionArgs = (ActionArgs) getArguments().getSerializable(KEY_ACTION);
        bookBeans = (List<BookBean>) getArguments().getSerializable(KEY_BOOKS);
        viewModel.showSupport.postValue(getArguments().getBoolean(KEY_SHOW_SUPPORT));
        viewModel.setArgements(actionArgs, bookBeans);
    }

    @Override
    public void onResume() {
        super.onResume();
        DeviceHelper.getInstance().changeAnalyzeModel(AnalyzeImpl.TO_SHELF);
    }


    @Override
    public void initViewObservable() {
        viewModel.ui.codeLengthEvent.observe(this, integer -> binding.shelfCode.setSelection(integer));
        viewModel.ui.listingEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_SHELFS, shelfBeans);
            bundle.putSerializable(KEY_ACTION, actionArgs);
            NavHostFragment.findNavController(this).navigate(actionArgs.getFirst2second(), bundle);
        });

        viewModel.ui.forcePosEvent.observe(this, bookBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS, (Serializable) bookBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_FORCE);
            bundle.putBoolean(KEY_OP_STATE, bookBeans != null);
            bundle.putString(BundleKey.KEY_CURRENT_SHELF, viewModel.shelfName.getValue());
            bundle.putString(BundleKey.KEY_TITLE, "强制定位");
            NavHostFragment.findNavController(this).navigate(ACTION_FORCE.getResultAction(), bundle);
        });

        viewModel.ui.changeShelfEvent.observe(this, bookBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.KEY_BOOKS, (Serializable) bookBeans);
            bundle.putSerializable(BundleKey.KEY_ACTION, ACTION_CHANGE);
            bundle.putBoolean(KEY_OP_STATE, bookBeans != null);
            bundle.putString(BundleKey.KEY_CURRENT_SHELF, viewModel.shelfName.getValue());
            bundle.putString(BundleKey.KEY_TITLE, "层位倒架");
            NavHostFragment.findNavController(this).navigate(ACTION_CHANGE.getResultAction(), bundle);
        });

        viewModel.ui.inventoryEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_SHELFS, shelfBeans);
            bundle.putSerializable(KEY_ACTION, actionArgs);
            NavHostFragment.findNavController(this).navigate(actionArgs.getFirst2second(), bundle);
        });

        viewModel.ui.firstPosEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putString(KEY_SHELF_CODE, shelfBeans.getShelfCode());
            bundle.putSerializable(KEY_ACTION, actionArgs);
            NavHostFragment.findNavController(this).navigate(actionArgs.getFirst2second(), bundle);
        });

        viewModel.ui.parallelEvent.observe(this, shelfBeans -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(KEY_SHELFS, shelfBeans);
            bundle.putSerializable(KEY_ACTION, actionArgs);
            NavHostFragment.findNavController(this).navigate(actionArgs.getFirst2second(), bundle);
        });
        viewModel.ui.selectShelfEvent.observe(this, bean -> SelectShelfDialog.getInstance().showDialog(getActivity(), bean, bean1 -> {
            viewModel.updateShelf(bean1);
        }));
    }


}
